package web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import web.bean.Fangchan;
import web.bean.XiaoQu;
import web.dao.FangChanDao;
import web.dao.Impl.FangChanDaoImpl;
import web.service.FangChanService;
import web.service.Impl.FangChanServiceImpl;
import web.service.Impl.XiaoQuServiceImpl;
import web.service.XiaoQuService;
import web.utils.R;
import web.vo.PageVo;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class FangChanController {
    XiaoQuService xiaoQuService=new XiaoQuServiceImpl();
    FangChanService fangChanService=new FangChanServiceImpl();
    public R page(JSONObject jsonObject){
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer pageNo=jsonObject.getInteger("pageNo");
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");
        PageVo<Fangchan> byInfo = fangChanService.findByInfo(pageSize, pageNo, startTime, endTime, name);
        System.out.println(R.ok().setData(byInfo));
        return R.ok().setData(byInfo);
    }
    public R add(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        Fangchan fangchan = JSONObject.parseObject(s, Fangchan.class);
        Integer xiaoquId=fangchan.getXiaoquId();
        XiaoQu xiaoqu = xiaoQuService.findXiaoQuById(xiaoquId);
        fangchan.setSuoshuxiaoqu(xiaoqu.getXiaoqumingcheng());
        //调用service，录入一个新的小区
        int result = fangChanService.add(fangchan);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }
    }
    public R del(JSONObject jsonObject){
        JSONArray delid = jsonObject.getJSONArray("id");
        Integer[] deleteId = new Integer[delid.size()];
        for (int i = 0; i < delid.size(); i++) {
            deleteId[i]=delid.getInteger(i);
        }
        int result = fangChanService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }
    public R edit(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        Fangchan fangchan = JSONObject.parseObject(s, Fangchan.class);
        Integer xiaoquId=fangchan.getXiaoquId();
        XiaoQu xiaoqu = xiaoQuService.findXiaoQuById(xiaoquId);
        fangchan.setSuoshuxiaoqu(xiaoqu.getXiaoqumingcheng());
        int i = fangChanService.editInfo(fangchan);
        if(i>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }
    public R showFangChan(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        Fangchan fanchan = fangChanService.findFanChanById(id);
        return R.ok().setData(fanchan);
    }
    public R findNameAll(Map<String,String[]> map){
        List<Fangchan> fanchan = fangChanService.findNameAll();
        return R.ok().setData(fanchan);
    }
    public XiaoQu findNameById(Integer id){
        return xiaoQuService.findNameById(id);
    }
}
